⚡️ Speed up method AmazonBedrockEmbeddingFunction.build_from_config by 18%
#13
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 18% (0.18x) speedup for
AmazonBedrockEmbeddingFunction.build_from_configinchromadb/utils/embedding_functions/amazon_bedrock_embedding_function.py⏱️ Runtime :
542 microseconds→460 microseconds(best of60runs)📝 Explanation and details
The optimized code achieves a 17% speedup through several micro-optimizations that reduce Python bytecode operations:
Key optimizations:
Faster attribute access: Replaced
hasattr(session, attr) and session.attrwithgetattr(session, attr, None)for session args extraction. This eliminates duplicate attribute lookups and reduces opcodes.Optimized type checking: Added a
type(value) not in _primitive_typescheck before the more expensiveisinstance()call. For primitive types (the common case), this avoids the overhead ofisinstance()entirely.Simplified session creation: Collapsed the if-else branch for session creation into a single conditional expression
boto3.Session(**session_args) if session_args is not None else boto3.Session(), eliminating redundant branching.Import reorganization: Moved imports to follow PEP 8 style (stdlib before third-party), though this has minimal performance impact.
Performance characteristics from tests:
The optimizations are most effective when dealing with many kwargs or repeated calls, as the reduced per-item overhead compounds.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_aqrniplu/tmprus2lwk1/test_concolic_coverage.py::test_AmazonBedrockEmbeddingFunction_build_from_configTo edit these changes
git checkout codeflash/optimize-AmazonBedrockEmbeddingFunction.build_from_config-mh1l8qrqand push.